#include "stdafx.h"
#include "Input.h"
namespace FIRSTDX
{
	LONG InputController::MOUSE_X,InputController::MOUSE_Y;
	std::unordered_map<int, bool> InputController::keyMap;
	int InputController::trigger = 0;
	Mouse::Mouse( HWND hwnd):m_hWnd(hwnd)
	{
		assert(hwnd != NULL);
		// Initialize buttons to "up".
		//
		for( ButtonId button=0; button<MAX_BUTTONS; ++button)
		{
			m_buttonStates[ button ] = false;
			m_lastButtonStates[ button ] = false;
		}

	}
	unsigned int Mouse::getNumButtons() const
	{
		return MAX_BUTTONS;
	}
	vector2 Mouse::getPosition() const
	{
		POINT mousePos;
		GetCursorPos(&mousePos);
		ScreenToClient(m_hWnd,&mousePos);
		return vector2(static_cast<float>(mousePos.x),static_cast<float>(mousePos.y));
	}
	bool Mouse::didGoUp( ButtonId button) const
	{
		assert( button < getNumButtons() );
		return !m_buttonStates[ button ] && m_lastButtonStates[ button ];
	}
	bool Mouse::didGoDown( ButtonId button ) const
	{
		assert( button < getNumButtons() );
		return m_buttonStates[ button ] && !m_lastButtonStates[ button ];
	}
	bool Mouse::isDown( ButtonId button ) const
	{
		assert( button< getNumButtons());
		return m_buttonStates[ button ];
	}

	void Mouse::ProcessMessages(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
	{
		switch( message )
		{
		case WM_LBUTTONDOWN:
			m_buttonStates[0] = true;
			break;
		case WM_LBUTTONUP:
			m_buttonStates[0] = false;
			break;
		case WM_RBUTTONDOWN:
			m_buttonStates[1] = true;
			break;
		case WM_RBUTTONUP:
			m_buttonStates[1] = false;
			break;
		case WM_MBUTTONDOWN:
			m_buttonStates[2] = true;
			break;
		case WM_MBUTTONUP:
			m_buttonStates[2] = false;
			break;
		}
	}
	Mouse& GetTheMouse(HWND hWnd)
	{
		static Mouse mouse(hWnd);
		return mouse;
	}

	void Mouse::updateState()
	{
		for( ButtonId button=0; button<MAX_BUTTONS; ++button)
		{
			m_lastButtonStates[ button ] = m_buttonStates[ button ];
		}
	}

	void calculateCenter( HWND hwnd )
	{

		RECT cRect,wRect;
		GetClientRect( hwnd, &cRect );
		GetWindowRect(hwnd, &wRect);
		int border_thickness = (wRect.right - wRect.left - cRect.right) /2;

		LONG MOUSE_CENTER_X = cRect.right / 2 + border_thickness + wRect.left; 
		LONG MOUSE_CENTER_Y = (cRect.bottom - cRect.top +45) / 2 + wRect.top;
		InputController::setRect(MOUSE_CENTER_X, MOUSE_CENTER_Y);
		::SetCursorPos(MOUSE_CENTER_X,MOUSE_CENTER_Y);
	}

}